/*
 * TransiCast / Qt
 * (c) 2012 Joachim Pfeiffer
 * Please refer to http://code.google.com/p/transitacst for more information
 */
#include "tsandcs.hpp"

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QTextStream>
#include <QApplication>

TsAndCs::TsAndCs(QMainWindow *parent, QApplication *parentApp, const QString version)
    : QDialog(parent)
{
    tcsAndCsLabel = new QTextBrowser();
    tcsAndCsLabel->setEditFocus(false);
    cancelButton = new QPushButton(tr("Cancel"));
    okButton = new QPushButton(tr("OK"));
    connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
    connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));

    // Header, consisting of app icon and app title plus version
    QHBoxLayout *headerLayout = new QHBoxLayout();
    headerLayout->setAlignment(Qt::AlignCenter);
    QLabel *iconLabel = new QLabel();
    QPixmap appIconPixmap = QPixmap(":/appico.png");
    iconLabel->setPixmap(appIconPixmap);
    headerLayout->addWidget(iconLabel);
    headerLayout->addWidget(new QLabel("  TransiCast " + version));

    // Cancel and OK buttons
    QHBoxLayout *buttonsLayout = new QHBoxLayout();
    buttonsLayout->addWidget(cancelButton);
    buttonsLayout->addWidget(okButton);

    // Main layout, consisting of header, text box and buttons
    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->addLayout(headerLayout);
    mainLayout->addWidget(tcsAndCsLabel);
    mainLayout->addLayout(buttonsLayout);
    this->setLayout(mainLayout);

    // Show dialog box
    this->setAttribute(Qt::WA_TranslucentBackground, false);
    this->showMaximized();

    // Read ts-and-cs file
    QFile *tsandcsInFile = new QFile(":/tsandcs.txt");
    tsandcsInFile->open(QIODevice::ReadOnly | QIODevice::Text);
    QTextStream tsandcsIn(tsandcsInFile);
    QString tcsAndCs = "";
    while (!tsandcsIn.atEnd())
        tcsAndCs += tsandcsIn.readLine() + "\n";
    tcsAndCsLabel->setText(tcsAndCs);
    tsandcsInFile->close();
    tsandcsInFile->deleteLater();
}

void TsAndCs::accept()
{
    QFile *confFile = new QFile("transicast.conf");
    confFile->open(QIODevice::WriteOnly | QIODevice::Text);
    QTextStream out(confFile);
    out << "TC=accepted\n";
    confFile->close();
    confFile->deleteLater();
    QDialog::accept();
    this->deleteLater();
}

void TsAndCs::reject()
{
    exit(0);
}

